import 'package:check_app/dao/userInfo_dao.dart';
import 'package:check_app/page/MyMobilePage.dart';
import 'package:check_app/util/uploadImage_util.dart';
import 'package:check_app/widgets/downModal.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:modal_progress_hud/modal_progress_hud.dart';

class MyInfoPage extends StatefulWidget {
  final String name;
  final String code;
  final String typeName;
  final String mobile;
  final String avatarUrl;

  const MyInfoPage({
    Key key,
    this.name,
    this.code,
    this.typeName,
    this.mobile,
    this.avatarUrl,
  }) : super(key: key);
  @override
  State<StatefulWidget> createState() => new _MyInfoPageState();
}

class _MyInfoPageState extends State<MyInfoPage> {
  bool _loading = false;
  String currentMobile;
  String avatarUrl;
  @override
  void initState() {
    super.initState();
    currentMobile = widget.mobile;
    avatarUrl = widget.avatarUrl;
  }

  Future<Null> saveAvatarUrl(String url) async {
    setState(() {
      _loading = true;
    });
    try {
      var result = await UserInfoDao.save({'avatarUrl': url});
      setState(() {
        _loading = false;
      });
      if (result.success) {
        setState(() {
          avatarUrl = url;
        });
      } else {
        Fluttertoast.showToast(
          msg: result.message,
        );
      }
    } catch (e) {
      print(e);
      setState(() {
        _loading = false;
      });
    }
    return null;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color.fromRGBO(248, 248, 249, 1),
      appBar: AppBar(
        centerTitle: true,
        title: Text('个人资料'),
      ),
      body: SafeArea(
        child: ModalProgressHUD(
          inAsyncCall: _loading,
          child: Column(
            children: <Widget>[
              Container(
                margin: EdgeInsets.only(top: 12),
                padding: EdgeInsets.only(left: 16),
                decoration: BoxDecoration(
                  color: Color.fromRGBO(255, 255, 255, 1),
                  border: Border(
                    top: BorderSide(
                      width: 0.5,
                      color: Color.fromRGBO(229, 229, 229, 1),
                    ),
                    bottom: BorderSide(
                      width: 0.5,
                      color: Color.fromRGBO(229, 229, 229, 1),
                    ),
                  ),
                ),
                child: Column(
                  children: <Widget>[
                    // 头像
                    GestureDetector(
                      onTap: () {
                        showModalBottomSheet(
                          context: context,
                          builder: (BuildContext context) {
                            return DownModal(
                              cameraCb: () async {
                                final url = await UploadImageUtil()
                                    .getImageFromCamera();
                                saveAvatarUrl(url);
                              },
                              photoCb: () async {
                                final urlList =
                                    await UploadImageUtil().loadAssets(1);
                                saveAvatarUrl(urlList[0]);
                              },
                            );
                          },
                        );
                      },
                      child: Container(
                        height: 96,
                        padding: EdgeInsets.only(right: 16),
                        decoration: BoxDecoration(
                          color: Color.fromRGBO(255, 255, 255, 1),
                          border: Border(
                            bottom: BorderSide(
                              width: 0.5,
                              color: Color.fromRGBO(229, 229, 229, 1),
                            ),
                          ),
                        ),
                        child: Row(
                          children: <Widget>[
                            Text(
                              '头像',
                              style: TextStyle(
                                fontSize: 16,
                                height: 22 / 16,
                              ),
                            ),
                            Expanded(
                              child: Container(
                                margin: EdgeInsets.only(right: 12),
                                alignment: Alignment.centerRight,
                                child: CircleAvatar(
                                  radius: 28.0,
                                  backgroundImage: avatarUrl == null
                                      ? AssetImage('assets/images/touxiang.png')
                                      : NetworkImage(avatarUrl),
                                ),
                              ),
                            ),
                            Image(
                              image:
                                  AssetImage('assets/images/chevron_gray.png'),
                              width: 8,
                              height: 13,
                            ),
                          ],
                        ),
                      ),
                    ),

                    // 用户名
                    Container(
                      height: 62,
                      padding: EdgeInsets.only(right: 16),
                      decoration: BoxDecoration(
                        color: Color.fromRGBO(255, 255, 255, 1),
                        border: Border(
                          bottom: BorderSide(
                            width: 0.5,
                            color: Color.fromRGBO(229, 229, 229, 1),
                          ),
                        ),
                      ),
                      child: Row(
                        children: <Widget>[
                          Text(
                            '用户名',
                            style: TextStyle(
                              fontSize: 16,
                              height: 22 / 16,
                            ),
                          ),
                          Expanded(
                            child: Container(
                              alignment: Alignment.centerRight,
                              child: Text(
                                widget.name ?? '--',
                                style: TextStyle(
                                  fontSize: 16,
                                  height: 22 / 16,
                                  color: Color.fromRGBO(150, 151, 154, 1),
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                    // 用户编码
                    Container(
                      height: 62,
                      padding: EdgeInsets.only(right: 16),
                      decoration: BoxDecoration(
                        color: Color.fromRGBO(255, 255, 255, 1),
                        border: Border(
                          bottom: BorderSide(
                            width: 0.5,
                            color: Color.fromRGBO(229, 229, 229, 1),
                          ),
                        ),
                      ),
                      child: Row(
                        children: <Widget>[
                          Text(
                            '用户编码',
                            style: TextStyle(
                              fontSize: 16,
                              height: 22 / 16,
                            ),
                          ),
                          Expanded(
                            child: Container(
                              alignment: Alignment.centerRight,
                              child: Text(
                                widget.code ?? '--',
                                style: TextStyle(
                                  fontSize: 16,
                                  height: 22 / 16,
                                  color: Color.fromRGBO(150, 151, 154, 1),
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                    // 用户类型
                    Container(
                      height: 62,
                      padding: EdgeInsets.only(right: 16),
                      decoration: BoxDecoration(
                        color: Color.fromRGBO(255, 255, 255, 1),
                        border: Border(
                          bottom: BorderSide(
                            width: 0.5,
                            color: Color.fromRGBO(229, 229, 229, 1),
                          ),
                        ),
                      ),
                      child: Row(
                        children: <Widget>[
                          Text(
                            '用户类型',
                            style: TextStyle(
                              fontSize: 16,
                              height: 22 / 16,
                            ),
                          ),
                          Expanded(
                            child: Container(
                              alignment: Alignment.centerRight,
                              child: Text(
                                widget.typeName ?? '--',
                                style: TextStyle(
                                  fontSize: 16,
                                  height: 22 / 16,
                                  color: Color.fromRGBO(150, 151, 154, 1),
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                    // 联系方式
                    GestureDetector(
                      onTap: () async {
                        final result = await Navigator.of(context)
                            .push(MaterialPageRoute(builder: (_) {
                          return MyMobilePage();
                        }));
                        if (result != null && result != '') {
                          setState(() {
                            currentMobile = result;
                          });
                        }
                      },
                      child: Container(
                        height: 62,
                        padding: EdgeInsets.only(right: 16),
                        color: Color.fromRGBO(255, 255, 255, 1),
                        child: Row(
                          children: <Widget>[
                            Text(
                              '联系方式',
                              style: TextStyle(
                                fontSize: 16,
                                height: 22 / 16,
                              ),
                            ),
                            Expanded(
                              child: Container(
                                margin: EdgeInsets.only(right: 12),
                                alignment: Alignment.centerRight,
                                child: Text(
                                  currentMobile ?? '--',
                                  style: TextStyle(
                                    fontSize: 16,
                                    height: 22 / 16,
                                    color: Color.fromRGBO(150, 151, 154, 1),
                                  ),
                                ),
                              ),
                            ),
                            Image(
                              image:
                                  AssetImage('assets/images/chevron_gray.png'),
                              width: 8,
                              height: 13,
                            ),
                          ],
                        ),
                      ),
                    )
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
